using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Xml;
using Newtonsoft.Json.Linq;

namespace BUS
{
	/// <summary> 
	///Author: nnamthach@gmail.com 
	/// <summary>
    public class OpNgayControl : BUSControl
    {
		#region Local Variable
        private OpNgayDataAccess _objDAO;
		#endregion Local Variable

	    public OpNgayControl(string type, string connectString, int timeout = 0)
            : base(type, connectString, timeout) { _objDAO = new OpNgayDataAccess(Connnection); }
			
        public OpNgayControl(BUSControl common)
            : base(common) { _objDAO = new OpNgayDataAccess(common.Connnection); }
	
		#region Method
		public DataTable GetShortData(String XuongId, List<DTO.FilterParameter> filters, ref string sErr) {
            return _objDAO.GetShortData(XuongId, filters, ref sErr);
        }
       		
        public DataTable GetByFilterToDataTable(String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0) {
            return _objDAO.GetByFilterToDataTable(XuongId, filters, ref logMsg, indexPage, itemPerPage);
        }
		
		public int GetCountRecord(String XuongId, List<DTO.FilterParameter> filters, ref string sErr) {
            return _objDAO.GetCountRecord(XuongId, filters, ref sErr);
        }
		
		public OpNgayInfo[] GetByFilter(String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0) {
            OpNgayInfo[] res = _objDAO.GetByFilter(XuongId, filters, ref logMsg, indexPage, itemPerPage);
			//get details
			
			return res;
        }
		
        public int Add(OpNgayInfo obj, ref string sErr) {
            _objDAO.Add(obj, ref sErr);
			if(sErr == "") {
				//add details
			}
			return 1;
        }
		
        public string Update(OpNgayInfo obj) {
            string sErr = _objDAO.Update(obj);
			// delete details
			// add details
			return sErr;
        }
		
        public string Delete(String XuongId, Int32 LineId) {
            string sErr = "";
			// delete details
			
			if(sErr == "") sErr = _objDAO.Delete(XuongId, LineId);
			return sErr;
        }  
        public bool IsExist(String XuongId, Int32? LineId, ref string sErr) {
            return _objDAO.IsExist(XuongId, LineId, ref sErr);
        } 

        public string InsertUpdate(OpNgayInfo obj)
        {
            string sErr = "";
            if (IsExist(obj.XuongId, obj.LineId, ref sErr)) { sErr = Update(obj); }
            else { 
				if(sErr =="") Add(obj, ref sErr); 
			}
            return sErr;
        }		
      
		public string TransferIn(DataRow row) {
            OpNgayInfo inf = new OpNgayInfo(row);
            return InsertUpdate(inf);
        }

        public override string ToXmlString(DTOInfo[] lInf, ref string sErr) {
            string result = "";
            try {
                if (lInf != null && lInf.Length > 0) {
                    XmlDocument doc = new XmlDocument();
                    XmlElement root = doc.CreateElement("OpNgay");
                    doc.AppendChild(root);
                    foreach (DTO.DTOInfo x in lInf) {
                        DTO.OpNgayInfo it = x as DTO.OpNgayInfo;
                        if (it == null) return "";

                        XmlElement row = doc.CreateElement("row");

                        if (it.LineId != null) row.SetAttribute("LineId", string.Format("{0}", it.LineId));
						if (it.Ngay1 != null) row.SetAttribute("Ngay1", string.Format("{0}", it.Ngay1));
						if (it.Ngay2 != null) row.SetAttribute("Ngay2", string.Format("{0}", it.Ngay2));
						if (it.Ngay3 != null) row.SetAttribute("Ngay3", string.Format("{0}", it.Ngay3));
						if (it.Ngay4 != null) row.SetAttribute("Ngay4", string.Format("{0}", it.Ngay4));
						if (it.Ngay5 != null) row.SetAttribute("Ngay5", string.Format("{0}", it.Ngay5));
						if (it.Ngay6 != null) row.SetAttribute("Ngay6", string.Format("{0}", it.Ngay6));
						if (it.Ngay7 != null) row.SetAttribute("Ngay7", string.Format("{0}", it.Ngay7));
						if (it.Ngay8 != null) row.SetAttribute("Ngay8", string.Format("{0}", it.Ngay8));
						if (it.Ngay9 != null) row.SetAttribute("Ngay9", string.Format("{0}", it.Ngay9));
						if (it.Ngay10 != null) row.SetAttribute("Ngay10", string.Format("{0}", it.Ngay10));
						if (it.Ngay11 != null) row.SetAttribute("Ngay11", string.Format("{0}", it.Ngay11));
						if (it.Ngay12 != null) row.SetAttribute("Ngay12", string.Format("{0}", it.Ngay12));
						if (it.Ngay13 != null) row.SetAttribute("Ngay13", string.Format("{0}", it.Ngay13));
						if (it.Ngay14 != null) row.SetAttribute("Ngay14", string.Format("{0}", it.Ngay14));
						if (it.Ngay15 != null) row.SetAttribute("Ngay15", string.Format("{0}", it.Ngay15));
						if (it.Ngay16 != null) row.SetAttribute("Ngay16", string.Format("{0}", it.Ngay16));
						if (it.Ngay17 != null) row.SetAttribute("Ngay17", string.Format("{0}", it.Ngay17));
						if (it.Ngay18 != null) row.SetAttribute("Ngay18", string.Format("{0}", it.Ngay18));
						if (it.Ngay19 != null) row.SetAttribute("Ngay19", string.Format("{0}", it.Ngay19));
						if (it.Ngay20 != null) row.SetAttribute("Ngay20", string.Format("{0}", it.Ngay20));
						if (it.Ngay21 != null) row.SetAttribute("Ngay21", string.Format("{0}", it.Ngay21));
						if (it.Ngay22 != null) row.SetAttribute("Ngay22", string.Format("{0}", it.Ngay22));
						if (it.Ngay23 != null) row.SetAttribute("Ngay23", string.Format("{0}", it.Ngay23));
						if (it.Ngay24 != null) row.SetAttribute("Ngay24", string.Format("{0}", it.Ngay24));
						if (it.Ngay25 != null) row.SetAttribute("Ngay25", string.Format("{0}", it.Ngay25));
						if (it.Ngay26 != null) row.SetAttribute("Ngay26", string.Format("{0}", it.Ngay26));
						if (it.Ngay27 != null) row.SetAttribute("Ngay27", string.Format("{0}", it.Ngay27));
						if (it.Ngay28 != null) row.SetAttribute("Ngay28", string.Format("{0}", it.Ngay28));
						if (it.Ngay29 != null) row.SetAttribute("Ngay29", string.Format("{0}", it.Ngay29));
						if (it.Ngay30 != null) row.SetAttribute("Ngay30", string.Format("{0}", it.Ngay30));
						if (it.Ngay31 != null) row.SetAttribute("Ngay31", string.Format("{0}", it.Ngay31));
						// details
						//if (it.details != null) row.SetAttribute("details", JsonConvert.SerializeObject(it.details));

                        root.AppendChild(row);
                    }

                    result = doc.InnerXml;
                }
            }
            catch (Exception ex) { sErr = ex.Message; }
            return result;
        }

        public override string TransferIn(DTOInfo inf, BUS.BUSControl.TransferMode mode) {
            string sErr = "";
            if (inf is OpNgayInfo) {
                if (mode == BUS.BUSControl.TransferMode.AddNew) Add((DTO.OpNgayInfo)inf, ref sErr);
                else if (mode == BUS.BUSControl.TransferMode.Update) sErr = Update((OpNgayInfo)inf);
                else sErr = InsertUpdate((OpNgayInfo)inf);
            }
            return sErr;
        }

        public override DTO.DTOInfo[] Preview(XmlDocument xml,ref string sErr, string db = null) {
            DTO.DTOInfo[] lRes = null;
            XmlElement doc = xml.DocumentElement;
            lRes = new DTOInfo[doc.ChildNodes.Count];

            for (int i = 0; i < doc.ChildNodes.Count; i++) {
                if (sErr != "") return lRes;
                try {
                    XmlElement ele = doc.ChildNodes[i] as XmlElement;
               
                    DTO.OpNgayInfo inf = new DTO.OpNgayInfo();
					
					inf.XuongId = db;
					if (GetStringAttribute(ele, "LineId") != "") inf.LineId = GetInt32Attribute(ele, "LineId");
					if (GetStringAttribute(ele, "Ngay1") != "") inf.Ngay1 = GetInt32Attribute(ele, "Ngay1");
					if (GetStringAttribute(ele, "Ngay2") != "") inf.Ngay2 = GetInt32Attribute(ele, "Ngay2");
					if (GetStringAttribute(ele, "Ngay3") != "") inf.Ngay3 = GetInt32Attribute(ele, "Ngay3");
					if (GetStringAttribute(ele, "Ngay4") != "") inf.Ngay4 = GetInt32Attribute(ele, "Ngay4");
					if (GetStringAttribute(ele, "Ngay5") != "") inf.Ngay5 = GetInt32Attribute(ele, "Ngay5");
					if (GetStringAttribute(ele, "Ngay6") != "") inf.Ngay6 = GetInt32Attribute(ele, "Ngay6");
					if (GetStringAttribute(ele, "Ngay7") != "") inf.Ngay7 = GetInt32Attribute(ele, "Ngay7");
					if (GetStringAttribute(ele, "Ngay8") != "") inf.Ngay8 = GetInt32Attribute(ele, "Ngay8");
					if (GetStringAttribute(ele, "Ngay9") != "") inf.Ngay9 = GetInt32Attribute(ele, "Ngay9");
					if (GetStringAttribute(ele, "Ngay10") != "") inf.Ngay10 = GetInt32Attribute(ele, "Ngay10");
					if (GetStringAttribute(ele, "Ngay11") != "") inf.Ngay11 = GetInt32Attribute(ele, "Ngay11");
					if (GetStringAttribute(ele, "Ngay12") != "") inf.Ngay12 = GetInt32Attribute(ele, "Ngay12");
					if (GetStringAttribute(ele, "Ngay13") != "") inf.Ngay13 = GetInt32Attribute(ele, "Ngay13");
					if (GetStringAttribute(ele, "Ngay14") != "") inf.Ngay14 = GetInt32Attribute(ele, "Ngay14");
					if (GetStringAttribute(ele, "Ngay15") != "") inf.Ngay15 = GetInt32Attribute(ele, "Ngay15");
					if (GetStringAttribute(ele, "Ngay16") != "") inf.Ngay16 = GetInt32Attribute(ele, "Ngay16");
					if (GetStringAttribute(ele, "Ngay17") != "") inf.Ngay17 = GetInt32Attribute(ele, "Ngay17");
					if (GetStringAttribute(ele, "Ngay18") != "") inf.Ngay18 = GetInt32Attribute(ele, "Ngay18");
					if (GetStringAttribute(ele, "Ngay19") != "") inf.Ngay19 = GetInt32Attribute(ele, "Ngay19");
					if (GetStringAttribute(ele, "Ngay20") != "") inf.Ngay20 = GetInt32Attribute(ele, "Ngay20");
					if (GetStringAttribute(ele, "Ngay21") != "") inf.Ngay21 = GetInt32Attribute(ele, "Ngay21");
					if (GetStringAttribute(ele, "Ngay22") != "") inf.Ngay22 = GetInt32Attribute(ele, "Ngay22");
					if (GetStringAttribute(ele, "Ngay23") != "") inf.Ngay23 = GetInt32Attribute(ele, "Ngay23");
					if (GetStringAttribute(ele, "Ngay24") != "") inf.Ngay24 = GetInt32Attribute(ele, "Ngay24");
					if (GetStringAttribute(ele, "Ngay25") != "") inf.Ngay25 = GetInt32Attribute(ele, "Ngay25");
					if (GetStringAttribute(ele, "Ngay26") != "") inf.Ngay26 = GetInt32Attribute(ele, "Ngay26");
					if (GetStringAttribute(ele, "Ngay27") != "") inf.Ngay27 = GetInt32Attribute(ele, "Ngay27");
					if (GetStringAttribute(ele, "Ngay28") != "") inf.Ngay28 = GetInt32Attribute(ele, "Ngay28");
					if (GetStringAttribute(ele, "Ngay29") != "") inf.Ngay29 = GetInt32Attribute(ele, "Ngay29");
					if (GetStringAttribute(ele, "Ngay30") != "") inf.Ngay30 = GetInt32Attribute(ele, "Ngay30");
					if (GetStringAttribute(ele, "Ngay31") != "") inf.Ngay31 = GetInt32Attribute(ele, "Ngay31");
					// details
					//string details = GetStringAttribute(ele, "details");
                    //inf.details = JsonConvert.DeserializeObject<CsDataAccessGroupInfo.RoleDetail[]>(details);
                    lRes[i] = inf;
                }
                catch(Exception ex) { sErr = ex.Message; }
            }
            return lRes;
        }
		public bool validate(JObject o, bool pkOnly, ref string sErr) {
            bool inputErr = false;
			if (pkOnly) {
                if (o["LineId"] == null) { sErr += "Invalid LineId. Please check again"; inputErr = true; }
            } else {
				if (o["LineId"] == null) { sErr += "Invalid LineId. Please check again"; inputErr = true; }
				if (o["Ngay1"] != null) { }//Check validate 
				if (o["Ngay2"] != null) { }//Check validate 
				if (o["Ngay3"] != null) { }//Check validate 
				if (o["Ngay4"] != null) { }//Check validate 
				if (o["Ngay5"] != null) { }//Check validate 
				if (o["Ngay6"] != null) { }//Check validate 
				if (o["Ngay7"] != null) { }//Check validate 
				if (o["Ngay8"] != null) { }//Check validate 
				if (o["Ngay9"] != null) { }//Check validate 
				if (o["Ngay10"] != null) { }//Check validate 
				if (o["Ngay11"] != null) { }//Check validate 
				if (o["Ngay12"] != null) { }//Check validate 
				if (o["Ngay13"] != null) { }//Check validate 
				if (o["Ngay14"] != null) { }//Check validate 
				if (o["Ngay15"] != null) { }//Check validate 
				if (o["Ngay16"] != null) { }//Check validate 
				if (o["Ngay17"] != null) { }//Check validate 
				if (o["Ngay18"] != null) { }//Check validate 
				if (o["Ngay19"] != null) { }//Check validate 
				if (o["Ngay20"] != null) { }//Check validate 
				if (o["Ngay21"] != null) { }//Check validate 
				if (o["Ngay22"] != null) { }//Check validate 
				if (o["Ngay23"] != null) { }//Check validate 
				if (o["Ngay24"] != null) { }//Check validate 
				if (o["Ngay25"] != null) { }//Check validate 
				if (o["Ngay26"] != null) { }//Check validate 
				if (o["Ngay27"] != null) { }//Check validate 
				if (o["Ngay28"] != null) { }//Check validate 
				if (o["Ngay29"] != null) { }//Check validate 
				if (o["Ngay30"] != null) { }//Check validate 
				if (o["Ngay31"] != null) { }//Check validate 
			}
			return inputErr;
		}
		public override bool isField(string field) {
            return new DTO.OpNgayInfo().isField(field);
        }
		#endregion Method

    }
}
